package com.lancoo.answer.ui.ques.quesBody;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lancoo.answer.R;
import com.lancoo.answer.component.InteractWebView;
import com.lancoo.answer.model.AnswerConstant;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.TaskState;
import com.lancoo.answer.model.bean.Child;
import com.lancoo.answer.model.bean.Item;
import com.lancoo.answer.model.bean.Ques;
import com.lancoo.answer.model.bean.Type;
import com.lancoo.answer.util.paperUtils.FragmentCommunicationUtils;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * Created by dadong on 2021/12/16 9:30
 * Description:复合匹配大题题干加载fragment（匹配+简答，匹配+选择） 阅读综合 查看作答
 */
public class CompoundMatchQuseBodyLookingFragment extends Fragment {

    private static final String TAG = "CompoundMatchQuseBodyFr";
    private FrameLayout frameLayout;
    private InteractWebView interactWebView;
    private Ques ques;
    private String currentItemSortIndex = "";
    private int typeIndex;
    private int quesIndex;
    private int childIndex;
    private String itemSortIndex;
    private int itemIndex;

    public static Fragment loadQueseFragment(int kindIndex, int queseIndex, int childIndex, int defaultChildIndex,int defaultItemIndex) {
        CompoundMatchQuseBodyLookingFragment queseFragment = new CompoundMatchQuseBodyLookingFragment();
        Bundle mBundle = new Bundle();
        mBundle.putInt(AnswerConstant.KEY_TYPEINDEX, kindIndex);
        mBundle.putInt(AnswerConstant.KEY_QUESINDEX, queseIndex);
        mBundle.putInt(AnswerConstant.KEY_CHILDINDEX, childIndex);
        mBundle.putInt(AnswerConstant.KEY_LOADCHILDINDEX, defaultChildIndex);
        mBundle.putInt(AnswerConstant.KEY_LOADITEMINDEX, defaultItemIndex);
        queseFragment.setArguments(mBundle);
        return queseFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.ev_fragment_ques_body, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (getArguments() == null || getContext() == null || getActivity() == null) {
            return;
        }
        if (ConstantBean.Companion.getConstantExamBean() == null) {
            return;
        }
        List<Type> kindBeanListk = ConstantBean.Companion.getConstantExamBean().getTypeList();
        if (kindBeanListk == null) {
            return;
        }
        typeIndex = getArguments().getInt(AnswerConstant.KEY_TYPEINDEX);
        quesIndex = getArguments().getInt(AnswerConstant.KEY_QUESINDEX);
        childIndex = getArguments().getInt(AnswerConstant.KEY_CHILDINDEX);
        itemIndex = 0;
        if (kindBeanListk.get(typeIndex) == null ||
                kindBeanListk.get(typeIndex).getQuesList() == null) {
            return;
        }
        ques = kindBeanListk.get(typeIndex).getQuesList().get(quesIndex);
        interactWebView = new InteractWebView(getContext());
        if (ques == null || ques.getQuesBody() == null) {
            return;
        }
        frameLayout = view.findViewById(R.id.framelayout);
        frameLayout.addView(interactWebView);
        String quesBody = ques.getQuesBody();//大题题干
        if (TextUtils.isEmpty(quesBody)) {
            quesBody = ques.getChildList().get(0).getChildAsk();
        }
        Boolean isIndentation = ques.getIsIndentQuesBody();
        if (ConstantBean.getTaskState() == TaskState.ORIGINAL) {
            interactWebView.showOrHideAnalysisButton(0);
        } else {
            interactWebView.showOrHideAnalysisButton(1);
        }
        interactWebView.setQues(ques);
        interactWebView.setIsIndentation(isIndentation);
        interactWebView.initWebView(getActivity(), quesBody, new InteractWebView.Callback() {
            @Override
            public void clickCallback(@NotNull String position) {
                Log.e(TAG, "clickCallback position:" + position);
                itemSortIndex = position;
                wvLoadUrl(position);
                getRealIndex();
                EventBus.getDefault().post(getRealItem());
                FragmentCommunicationUtils.matchingSynthesisReposition(getParentFragment());
            }

            @Override
            public void finishCallback() {
                int index=0;
                if (getArguments() != null && getArguments().getInt(AnswerConstant.KEY_LOADITEMINDEX) >= 0) {
                    index = getArguments().getInt(AnswerConstant.KEY_LOADITEMINDEX);
                }
                itemSortIndex = ques.getChildList().get(childIndex).getItemList().get(index).getItemSortIndex();
                wvLoadUrl(itemSortIndex);
                EventBus.getDefault().post(getRealItem());
            }
        });
    }

    private void wvLoadUrl(String position) {
        if (interactWebView == null) {
            return;
        }
        Log.e(TAG, "wvLoadUrl:" + position);
        interactWebView.scrollPosition(position);
        for (Child it : ques.getChildList()) {
            if (ConstantBean.getTaskState() == TaskState.ORIGINAL) {
                for (Item item : it.getItemList()) {
                    interactWebView.loadOriginUrl(item,
                            position.equals(item.getItemSortIndex()),
                            true);
                }
            } else {
                for (Item item : it.getItemList()) {
                    interactWebView.loadUrl(item,
                            position.equals(item.getItemSortIndex()),
                            false,
                            true);
                }
            }
        }
    }

    public int getRealIndex() {
        int tempChildIndex = 0;
        for (Child it : ques.getChildList()) {
            int index = 0;
            for (Item item : it.getItemList()) {
                if (TextUtils.equals(itemSortIndex, item.getItemSortIndex())) {
                    itemIndex = item.getItemIndex();
                    childIndex = tempChildIndex;
                    return index;
                }
                index++;
            }
            tempChildIndex ++;
        }
        return 0;
    }


    //获取Item
    public Item getRealItem() {
        for (Item item : ques.getChildList().get(childIndex).getItemList()) {
            if (TextUtils.equals(itemSortIndex, item.getItemSortIndex())) {
                return item;
            }
        }
        return null;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (interactWebView != null) {
            interactWebView.release();
        }
        if (frameLayout != null) {
            frameLayout.removeAllViews();
        }
    }


    /**
     * 点击答题卡跳转
     * @param childIndex 小题下标
     * @param itemIndex  答题点下标
     */
    public void onClickItemIndex(int childIndex,int itemIndex) {
        itemSortIndex = ques.getChildList().get(childIndex).getItemList().get(itemIndex).getItemSortIndex();
        this.childIndex = childIndex;
        this.itemIndex = itemIndex;
        wvLoadUrl(itemSortIndex);
    }

    public void adjustFontSize(){
        if(interactWebView!=null){
            interactWebView.setFontSize();
        }
    }
}